package git.soulbgm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import git.soulbgm.common.anntation.Log;
import git.soulbgm.common.enums.OperationType;
import git.soulbgm.common.pojo.ReturnCode;
import org.springframework.web.bind.annotation.*;
import git.soulbgm.common.pojo.TableDataInfo;
import git.soulbgm.common.pojo.ResultData;
import git.soulbgm.pojo.UserInfo;
import git.soulbgm.service.UserInfoService;
import git.soulbgm.common.base.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * 用户信息表控制器
 *
 * @author SoulBGM
 * @date 2022-11-18 15:01:11
 */
@RestController
@RequestMapping("userInfo")
public class UserInfoController extends BaseController {

    private final UserInfoService service;

    public UserInfoController(UserInfoService service) {
        this.service = service;
    }

    @Log(title = "用户", type = OperationType.LOGIN, excludeRecordParam = "#password")
    @PostMapping("login")
    public ResultData login(String username, String password, HttpServletRequest request) {
        UserInfo userInfo = service.getOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername, username).eq(UserInfo::getPassword, password));
        if (userInfo != null) {
            request.getSession().setAttribute("user", userInfo);
            return success(ReturnCode.LOGIN_SUCCESS);
        }
        return error(ReturnCode.LOGIN_FAIL);
    }

    @GetMapping("list")
    public TableDataInfo list(UserInfo info) {
        startPage();
        List<UserInfo> list = service.list(new QueryWrapper<>(info));
        return getDataTable(list);
    }

    private final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    @Log(title = "用户信息", type = OperationType.EXPORT)
    @GetMapping("export")
    public void export(UserInfo info, HttpServletResponse response) throws IOException {
        List<UserInfo> list = service.list(new QueryWrapper<>(info));
        StringBuilder sb = new StringBuilder();
        list.forEach(o -> sb
                .append(o.getId()).append(",")
                .append(o.getUsername()).append(",")
                .append(o.getNickname()).append(",")
                .append(DATE_FORMAT.format(o.getCreateTime())).append("\n"));
        response.setContentType("text/csv");
        String fileName = "用户信息_" + System.currentTimeMillis();
        response.setHeader("Content-Disposition", String.format("attachment; filename=%s.csv", new String(fileName.getBytes("gb2312"), "ISO8859-1")));
        response.getOutputStream().write(sb.toString().getBytes(StandardCharsets.UTF_8));
    }

    @Log(title = "用户信息", type = OperationType.ADD, excludeRecordParam = {"#info.t1.name", "#info.t1.t2.name"})
    @PostMapping("add")
    public ResultData add(@RequestBody UserInfo info) {
        boolean result = service.save(info);
        return result ? success() : error();
    }

    @Log(title = "用户信息", type = OperationType.UPDATE)
    @PostMapping("edit")
    public ResultData edit(@RequestBody UserInfo info) {
        boolean result = service.updateById(info);
        return result ? success() : error();
    }

    @Log(title = "用户信息", type = OperationType.REMOVE)
    @PostMapping("remove")
    public ResultData remove(String ids) {
        boolean result = service.removeByIds(Arrays.asList(ids.split(",")));
        return result ? success() : error();
    }

}
